package zsxking.batchfilerenamer;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 * Data model of File list for Swing Table.
 * @author Shaoxuan Zhang
 *
 */
public class FileTableModel extends AbstractTableModel {
  
  /** Support Serialization. */
  private static final long serialVersionUID = -6495651531013465767L;
  private List<File> files = new ArrayList<File>();

  /**
   * @return the number of column in this table model.
   */
  public int getColumnCount() {
    return 3;
  }

  /**
   * @return the number of row in this table model.
   */
  public int getRowCount() {
    return getFiles().size();
  }

  /**
   * @param columnIndex the index.
   * @return the name of the column with given index.
   */
  @Override
  public String getColumnName(int columnIndex) {
    switch (columnIndex) {
    case 0: return "#";
    case 1: return "File Name";
    case 2: return "Path";
    default: return "";
    }
  }

  /**
   * @param row row index.
   * @param column column index.
   * @return the value at the given cell.
   */
  @Override
  public Object getValueAt(int rowIndex, int columnIndex) {
    File file = getFiles().get(rowIndex);
    //System.out.println("getting value at (" + row + "," + column + ")");
    switch (columnIndex) {
    case 0: return rowIndex + 1;
    case 1: return file.getName();
    case 2: return file.getPath();
    default: return "";
    }
  }

  /**
   * @return the files
   */
  List<File> getFiles() {
    return files;
  }

  /**
   * Remove file of the selected indices.
   * @param selectedIndices the selected indices.
   * @return the list of data removed.
   */
  public List<File> removeData(int[] selectedIndices) {
    //System.out.println("Removing " + selectedIndices.length + " data.");
    List<File> removeFiles = new ArrayList<File>();
    for (int i : selectedIndices) {
      removeFiles.add(files.get(i));
    }
    files.removeAll(removeFiles);
    return removeFiles;
  }
}
